package com.ss.android.article.base.feature.feed.a;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.StringRes;
import android.text.TextUtils;

import com.bytedance.article.common.c.JSONObjectWrapper;
import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.databinding.ObservableArrayList;
import com.bytedance.common.databinding.ObservableField;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.NetworkType;
import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.feed.activity.ArticleRecentFragment;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryObj;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryObj.CtrlFlag;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryThread;
import com.ss.android.article.base.feature.feed.presenter.ListData;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryHandler;
import com.ss.android.article.base.feature.feed.presenter.QueryCallback;
import com.ss.android.article.base.feature.feed.presenter.LastReadDataManager;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.base.feature.model.DownloadAppAd;
import com.ss.android.b.a.AdsAppItem;
import com.ss.android.common.app.AbsApplication;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.newmedia.BaseApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;

import inline.DrawerLayoutDemo;

public final class FeedDataProvider implements DrawerLayoutDemo.b, QueryCallback {
    final static class a {
        String a;
        String b;
        long c;
        JSONObject d;

        public a(String arg2, String arg3, long arg4, JSONObject arg6) {
            this.a = arg2;
            this.b = arg3;
            this.c = arg4;
            this.d = arg6;
        }
    }

    public static final class d$b {
        public AdsAppItem a;
        public String b;
        public boolean c;

        public d$b() {

        }

        public static d$b a(String arg1) {
            d$b v0 = new d$b();
            v0.b = arg1;
            return v0;
        }
    }

    public interface c {
        void onArticleListReceived(boolean arg1, boolean arg2, boolean arg3, boolean arg4);

        void onQueryNetwork(boolean arg1);

        int refreshList();

        void showNoDataView();
    }

    private long A = 0;
    private long B;
    private long C;
    private List D = new ArrayList();
    private long E = 0;
    private ArrayList F = new ArrayList();
    public WeakReference<ArticleRecentFragment> mRefArticleRecentFragment = new WeakReference(null);
    public CategoryInfo mCategoryInfo;
    public final ObservableField c = new ObservableField(Long.valueOf(0));
    public final ObservableField d = new ObservableField(Boolean.valueOf(false));
    public final ObservableField isRefresh = new ObservableField(Boolean.valueOf(false));
    public final ObservableArrayList f = new ObservableArrayList();
    public final ObservableArrayList mCellRefList = new ObservableArrayList();
    public final ObservableField<ListData> h = new ObservableField(new ListData());
    public final ObservableField i = new ObservableField(null);
    public int j = 0;
    public final ArrayList k = new ArrayList();
    public WeakReference<ArticleQueryThread> mRefArticleQueryThread = null;
    public int m = 0;
    public boolean n = false;
    public final ArticleQueryHandler o = new ArticleQueryHandler(this);
    public boolean p = false;
    public boolean q = false;
    public boolean r = false;
    public boolean s = false;
    public long t = 0;
    public long u = 0;
    public long v = 0;
    public long w = 0;
    private ObservableField x = new ObservableField(Boolean.valueOf(false));
    private ObservableField y = new ObservableField(Boolean.valueOf(false));
    private ObservableArrayList z = new ObservableArrayList();

    private static String getString(@StringRes int resId) {
        return BaseApplication.getApplication().getResources().getString(resId);
    }

    private void a(Context arg11, String arg12, String arg13, long arg14, JSONObject arg16) {
        if (this.s) {
            this.D.add(new a(arg12, arg13, arg14, arg16));
        } else {
            MobClickCombiner.onEvent(arg11, arg12, arg13, arg14, 0, arg16);
        }
    }

    private static void a(List arg2) {
        arg2.add(new CellRef(-11));
    }

    private void a(boolean arg3) {
        this.A = arg3 ? System.currentTimeMillis() : 0;
    }

    private void onArticleListReceived(boolean arg2, boolean arg3, boolean arg4, boolean arg5) {
        if (this.mRefArticleRecentFragment.get() != null) {
            this.mRefArticleRecentFragment.get().onArticleListReceived(arg2, arg3, arg4, arg5);
        }
    }

    public final void a(FeedRefreshFromInfo feedRefreshFromInfo, boolean force) {
        if (force) {
            this.d.set(Boolean.valueOf(false));
            ++this.j;
        }

        if (this.a(force, feedRefreshFromInfo.c)) {
            this.a(feedRefreshFromInfo);
        }
    }

    public final void cancelPreviousQueryThread() {
        ArticleQueryThread articleQueryThread = this.mRefArticleQueryThread != null ? this.mRefArticleQueryThread.get() : null;
        if (articleQueryThread != null) {
            articleQueryThread.cancel();
        }
        this.mRefArticleQueryThread = null;
    }

    public final void a(CategoryInfo categoryInfo) {
        this.mCategoryInfo = categoryInfo;
        if (this.g()) {
            LastReadDataManager.LastReadData v0 = android.support.design.a.j().a(categoryInfo.mCategoryName);
            if (v0 != null) {
                this.B = v0.time;
                this.C = v0.nextBehotTime;
            }
        }
    }

    public final void a(FeedRefreshFromInfo feedRefreshFromInfo) {
        ArticleQueryObj v12_1;
        boolean v10_1;
        long v30;
        long v8;
        if (feedRefreshFromInfo.c) {
            this.E = System.currentTimeMillis();
        }

        this.p = false;
        this.isRefresh.set(Boolean.valueOf(true));
        int v10 = feedRefreshFromInfo.refreshFrom;
        String v11 = feedRefreshFromInfo.refreshFromString;
        if (v10 == 0) {
            this.n = true;
            v11 = "enter_auto";
        } else {
            this.n = false;
        }

        if (v10 == 4) {
            if (AppData.getInstance().getAbSettings().isAppLogOld()) {
                this.a("refresh_auto");
            }

            if (!AppData.getInstance().getAbSettings().isAppLogNew()) {
            } else {
                this.a("auto", null);
            }
        } else {
            if (v10 == 5) {
                if (AppData.getInstance().getAbSettings().isAppLogOld()) {
                    this.a("tip_refresh");
                    this.a("refresh_pull");
                }

                if (!AppData.getInstance().getAbSettings().isAppLogNew()) {
                } else {
                    this.a("tab_tip", null);
                    this.a("pull", null);
                }
            } else if (v10 == 6) {
                if (!AppData.getInstance().getAbSettings().isAppLogNew()) {
                } else {
                    this.a("last_read", null);
                }
            } else if (v10 != 9) {
            } else {
                this.a("refresh_click_other_tip");
            }
        }

        this.d.set(Boolean.valueOf(true));
        ++this.j;
        long v6 = 0;
        long v4 = 0;
        if ((this.h()) || (((ListData) this.h.get()).e)) {
            v30 = v4;
        } else {
            v6 = ((CellRef) this.mCellRefList.get(0)).cateBehotTime;
            v8 = ((CellRef) this.mCellRefList.get(0)).cateCursor;
            v4 = this.h.get().g > v6 ? ((ListData) this.h.get()).g : v6;
            Logger.v("FeedDataProvider", "query top: " + this.h.get().g + " " + v6);
            v30 = v8;
            v6 = v4;
        }

        if (TextUtils.isEmpty(v11)) {
            switch (v10) {
                case 4: {
                    v11 = "auto";
                    break;
                }
                case 5: {
                    v11 = "tip";
                    break;
                }
                case 6: {
                    v11 = "last_read";
                    break;
                }
                case 7: {
                    v11 = "pull";
                    break;
                }
            }
        }
        boolean v5 = false;
        if (NetworkUtils.getNetworkType(BaseApplication.getApplication()) == NetworkType.NONE) {
            v5 = true;
        }

        boolean v3 = (v5) || !this.h() ? false : true;
        if (this.h.get() != null && ((ListData) this.h.get()).g > 0) {
            v3 = false;
        }

        if (this.mCategoryInfo.mIsFromConcernDetailVideo) {
            v5 = false;
            v10_1 = false;
        } else {
            v10_1 = v3;
        }

        int refreshReason = this.j;
        String categoryName = this.mCategoryInfo.mCategoryName;
        v8 = 0;
        String extra = this.mCategoryInfo.mExtra;
        EnumSet v13 = this.mCategoryInfo.mOnVideoTab ? EnumSet.of(CtrlFlag.onVideoTab) : null;
        ArticleQueryObj articleQueryObj = new ArticleQueryObj(refreshReason, categoryName, v5, v6,
                v8, v10_1, v11, extra, v13, this.mCategoryInfo.mReferType, ((Long) this.c.get()).longValue());
        articleQueryObj.r = this.k;
        articleQueryObj.isRefreshing = true;
        if (this.mCategoryInfo.mForumMovieId > 0) {
            articleQueryObj.movieId = this.mCategoryInfo.mForumMovieId;
        }

        if (this.mCategoryInfo.mIsFromConcernDetailVideo) {
            int v13_1 = this.j;
            String v14 = this.mCategoryInfo.mCategoryName;
            long v18 = 0;
            String v23 = this.mCategoryInfo.mExtra;
            EnumSet v24 = this.mCategoryInfo.mOnVideoTab ? EnumSet.of(CtrlFlag.onVideoTab) : null;
            v12_1 = new ArticleQueryObj(v13_1, v14, v5, v6, v18, 0, v10_1, v11, v23, v24,
                    this.mCategoryInfo.mReferType, ((Long) this.c.get()).longValue(), this.mCategoryInfo.mConcernDetailVideoQueryDict);
        } else {
            v12_1 = articleQueryObj;
        }

        articleQueryObj = this.mCategoryInfo.mWendaReferType != -1
                ? new ArticleQueryObj(this.j, this.mCategoryInfo.mCategoryName, v5, v6, 0,
                v10_1, v11, this.mCategoryInfo.mExtra, this.mCategoryInfo.mWendaReferType,
                ((Long) this.c.get()).longValue(), this.mCategoryInfo.mApiParam, v30)
                : v12_1;
        switch (feedRefreshFromInfo.refreshFrom) {
            case 2: {
                refreshReason = 4;
                break;
            }
            case 1:
            case 3: {
                refreshReason = 2;
                break;
            }
            case 0:
            case 4:
            case 5: {
                refreshReason = 5;
                break;
            }
            case 6: {
                refreshReason = 3;
                break;
            }
            case 7: {
                refreshReason = 1;
                break;
            }
            case 10: {
                refreshReason = 6;
                break;
            }
            default: {
                refreshReason = 0;
                break;
            }
        }

        articleQueryObj.refreshReason = refreshReason;
        articleQueryObj.O = feedRefreshFromInfo.c;
        ArticleQueryThread articleQueryThread = new ArticleQueryThread(BaseApplication.getApplication(), this.o, articleQueryObj);
        articleQueryThread.start();
        this.cancelPreviousQueryThread();
        this.mRefArticleQueryThread = new WeakReference(articleQueryThread);
        if ("__all__".equals(this.mCategoryInfo.mCategoryName)) {
            AppData.getInstance().q();
        }

        if (!articleQueryThread.needTryLocal()) {
            this.x.set(Boolean.valueOf(true));
        } else {
            this.x.set(Boolean.valueOf(false));
        }
    }

    public final void a(String arg8) {
        String v3;
        String v2 = "new_tab";
        if (!"__all__".equals(this.mCategoryInfo.mCategoryName)) {
            v2 = "category";
            if (this.mCategoryInfo.mCategoryName != null) {
                v3 = arg8 + "_" + this.mCategoryInfo.mCategoryName;
            } else {
                v3 = arg8;
            }
        } else {
            v3 = arg8;
        }

        com.ss.android.article.base.utils.JSONObjectWrapper v0 = new com.ss.android.article.base.utils.JSONObjectWrapper();
        v0.put("category_id", this.mCategoryInfo.mCategoryName);
        v0.put("refer", this.mCategoryInfo.mReferType);
        v0.put("concern_id", this.c.get());
        this.a(BaseApplication.getApplication(), v2, v3, 0, v0.jsonObject);
    }

    public final void a(String refreshType, JSONObject jo) {
        JSONObjectWrapper joWrapper = new JSONObjectWrapper();
        joWrapper.put("category_name", this.mCategoryInfo.mCategoryName).put("refresh_type", refreshType);
        if (AppData.getInstance().getAbSettings().isApplogStaging()) {
            joWrapper.put("_staging_flag", Integer.valueOf(1));
        }

        joWrapper.put(jo);
        if ((AppData.getInstance().getAbSettings().isSilenceRefreshOpen()) && (("auto".equals(refreshType)) || ("enter_auto".equals(refreshType)))) {
            String v2 = "auto_type";
            String v0 = AppData.getInstance().bp ? "silence" : "background";
            joWrapper.put(v2, v0);
            AppData.getInstance().bp = false;
        }
        android.support.design.a.onEvent("category_refresh", joWrapper.jo);
    }

    public final boolean a(boolean arg5, boolean arg6) {
        boolean v0;
        if (this.mCategoryInfo == null) {
            v0 = false;
        } else if (((Boolean) this.d.get()).booleanValue()) {
            if (!arg6) {
                this.i.set(d$b.a(getString(com.ss.android.article.browser.R.string.ss_hint_loading)));
            }

            v0 = false;
        } else {
            if (arg5) {
                this.isRefresh.set(Boolean.valueOf(true));
            }

            if (this.h()) {
                this.isRefresh.set(Boolean.valueOf(true));
            }

            if (NetworkUtils.getNetworkType(BaseApplication.getApplication()) != NetworkType.NONE) {
                if (((Boolean) this.isRefresh.get()).booleanValue()) {
                    return true;
                }

                if (((ListData) this.h.get()).c) {
                    return true;
                }

                v0 = false;
            } else if (((Boolean) this.isRefresh.get()).booleanValue()) {
                if (this.h()) {
                    if (!this.h()) {
                    } else if (!((ListData) this.h.get()).d) {
                        this.isRefresh.set(Boolean.valueOf(false));
                        this.d.notifyChange();
                        if (!arg6) {
                            this.i.set(d$b.a(getString(com.ss.android.article.browser.R.string.net_unvailable)));
                        }

                        v0 = false;
                        return false;
                    }
                    return true;
                } else {

                    label_43:
                    this.isRefresh.set(Boolean.valueOf(false));
                    this.d.notifyChange();
                    if (!arg6) {
                        this.i.set(d$b.a(getString(com.ss.android.article.browser.R.string.net_unvailable)));
                    }

                    v0 = false;
                }
            } else {
                if (((ListData) this.h.get()).d) {
                    return true;
                }

                v0 = false;
            }

            return v0;
        }
        return v0;
    }

    public final void onStartQuery(ArticleQueryObj articleQueryObj) {
        JSONObject v7;
        JSONObject v0_1 = null;
        long v4 = 0;
        JSONObject v6 = null;
        if (articleQueryObj != null && this.j == articleQueryObj.b) {
            if (this.mRefArticleRecentFragment.get() != null) {
                this.mRefArticleRecentFragment.get().onQueryNetwork(articleQueryObj.isRefreshing);
            }

            if (!this.n) {
                return;
            }

            try {
                v0_1 = new JSONObject();
                v0_1.put("category_id", this.mCategoryInfo.mCategoryName);
                v0_1.put("concern_id", this.c.get());
                v0_1.put("refer", 1);
                v7 = v0_1;
            } catch (JSONException v1) {
                v7 = v0_1;
            }

            if (AppData.getInstance().getAbSettings().isAppLogOld()) {
                if ("__all__".equals(this.mCategoryInfo.mCategoryName)) {
                    this.a(BaseApplication.getApplication(), "new_tab",
                            "refresh_enter_auto", v4, v6);
                } else {
                    this.a(BaseApplication.getApplication(), "category",
                            "refresh_enter_auto_" + this.mCategoryInfo.mCategoryName, v4, v7);
                }
            }

            if (AppData.getInstance().getAbSettings().isAppLogNew()) {
                this.a("enter_auto", v7);
            }
        }
    }

    public final void onFinishQuery(boolean arg26, ArticleQueryObj articleQueryObj) {
        boolean v4_2;
        String v7_3;
        List v6_4;
        int v7_1;
        List v24;
        int v6_3;
        long v18;
        long v16;
        long v14;
        SharedPreferences.Editor v5_2;
        String v2_4;
        d$b v4_1;
        ObservableField v2_3;
        boolean v2_2;
        int v9;
        Object v2_1;
        Iterator v6;
        int v2;
        int v3;
        if (articleQueryObj != null && this.j == articleQueryObj.b) {
            boolean v12 = articleQueryObj.O;
            articleQueryObj.O = false;
            if ((AppData.getInstance().D()) && (articleQueryObj.isRefreshing) && this.mCategoryInfo.l == 1) {
                this.c.set(Long.valueOf(articleQueryObj.E));
                "news_local".equals(this.mCategoryInfo.mCategoryName);
            } else if ((AppData.getInstance().D()) && (articleQueryObj.isRefreshing)) {
                v2 = com.ss.android.article.common.b.b.a().a != null ? 1 : 0;
                if (v2 == 0) {
                } else if (this.mCategoryInfo.l != 2) {
                } else {
                    this.c.set(Long.valueOf(articleQueryObj.E));
                    "news_local".equals(this.mCategoryInfo.mCategoryName);
                }
            }

            int v4 = !((Boolean) this.isRefresh.get()).booleanValue() || !arg26 ? 0 : 1;
            int v5 = articleQueryObj.actionToLastStick;
            switch (v5) {
                case 1: {
                    if (this.k.size() == 0) {
                        v9 = 0;
                        break;
                    }

                    v6 = this.k.iterator();
                    for (v3 = 0; v6.hasNext(); v3 = v2) {
                        v2_1 = v6.next();
                        if (this.mCellRefList.size() > 0) {
                            this.mCellRefList.remove(v2_1);
                            this.z.remove(v2_1);
                            v2 = 1;
                        } else {
                            v2 = v3;
                        }
                    }

                    v9 = v3;
                    break;
                }
                case 2: {
                    if (this.k.size() == 0) {
                        v9 = 0;
                        break;
                    }

                    v6 = this.k.iterator();

                    for (v3 = 0; v6.hasNext(); v3 = v3) {
                        v2_1 = v6.next();
                        ((CellRef) v2_1).stickStyle = 0;
                        if (this.mCellRefList.size() > 0 && (this.mCellRefList.contains(v2_1))) {
                            ((CellRef) this.mCellRefList.get(this.mCellRefList.indexOf(v2_1))).stickStyle = 0;
                            v3 = 1;
                            ((CellRef) this.z.get(this.z.indexOf(v2_1))).stickStyle = 0;
                        }
                    }

                    v9 = v3;
                    break;
                }
                default: {
                    label_46:
                    v9 = 0;
                    break;
                }
            }

            if (v4 != 0) {
                this.k.clear();
                if (articleQueryObj.s != null) {
                    this.k.addAll(articleQueryObj.s);
                }
            } else if (v5 > 0) {
                this.k.clear();
            }

            if ((((Boolean) this.isRefresh.get()).booleanValue()) && ("news_local".equals(this.mCategoryInfo.mCategoryName))) {
                ObservableField v3_1 = this.y;
                v2_2 = (articleQueryObj.feedFlag & 1) > 0 ? true : false;
                v3_1.set(Boolean.valueOf(v2_2));
            }

            if (articleQueryObj.maxBehotTime > 0) {
                this.b(articleQueryObj);
            }

            if (((Boolean) this.isRefresh.get()).booleanValue()) {
                this.b(articleQueryObj);
            } else {
                this.a(true);
            }

            String v5_1 = "__all__".equals(this.mCategoryInfo.mCategoryName) ? "newtab" : "category";
            String v6_1 = ((Boolean) this.isRefresh.get()).booleanValue() ? "refresh" : "load_more";
            if (!arg26) {
                if (this.q) {
                    this.r = true;
                    if (this.F.size() > 0) {
                        this.mCellRefList.clear();
                        this.mCellRefList.addAll(this.F);
                        this.f();
                        this.h.notifyChange();
                        this.F.clear();
                    }

                    this.q = false;
                }

                if ((articleQueryObj.errorCode != 19 || (this.h())) && this.mRefArticleRecentFragment.get() != null) {
                    v2_3 = this.i;
                    v3 = TTUtils.a(articleQueryObj.errorCode);
                    v4_1 = new d$b();
                    v4_1.b = AbsApplication.getApplication().getString(v3);
                    v2_3.set(v4_1);
                }

                if (((Boolean) this.isRefresh.get()).booleanValue()) {
                    this.isRefresh.set(Boolean.valueOf(false));
                }

                switch (articleQueryObj.errorCode) {
                    case 12: {
                        v2_4 = "no_connections";
                        break;
                    }
                    case 13: {
                        v2_4 = "connect_timeout";
                        break;
                    }
                    case 14: {
                        v2_4 = "network_timeout";
                        break;
                    }
                    case 15: {
                        v2_4 = "network_error";
                        break;
                    }
                    case 16: {
                        v2_4 = "server_error";
                        break;
                    }
                    case 17: {
                        v2_4 = "api_error";
                        break;
                    }
                    case 19: {
                        v2_4 = "service_unavailable";
                        break;
                    }
                    default: {
                        v2_4 = "unknown_error";
                        break;
                    }
                }

                this.a(BaseApplication.getApplication(), "load_status", v5_1 + "_"
                        + v6_1 + "_" + v2_4, ((long) articleQueryObj.cid), android.support.design.a.sendLoadStatusEvent(BaseApplication.getApplication(), articleQueryObj));
                if (v9 != 0) {
                    this.f();
                }

                this.d.set(Boolean.valueOf(false));
                this.onArticleListReceived(articleQueryObj.isRefreshing, articleQueryObj.v, false, false);
                return;
            }

            this.a(BaseApplication.getApplication(), "load_status", v5_1 + "_" + v6_1 + "_done", 0, android.support.design.a.sendLoadStatusEvent(BaseApplication.getApplication(), articleQueryObj));
            boolean v3_2 = !articleQueryObj.j || !articleQueryObj.v ? false : true;
            v4 = (articleQueryObj.d) || (v3_2) ? 1 : 0;
            boolean v13 = this.h();
            b(articleQueryObj.q);
            List v7 = AppData.getInstance().a(articleQueryObj.q);
            new ArrayList();
            this.r = false;
            if (this.q) {
                this.mCellRefList.clear();
                this.r = true;
                this.q = false;
            }

            if (!v7.isEmpty()) {
                long v10 = articleQueryObj.u;
                if (!articleQueryObj.v) {
                    v10 = System.currentTimeMillis();
                    if (articleQueryObj.minBehotTime <= 0 && articleQueryObj.minBehotTime == 0 && articleQueryObj.maxBehotTime == 0 || articleQueryObj.minBehotTime > 0) {
                        if (!this.g()) {

                        } else {
                            if (this.C > 0) {
                                this.B = this.C;
                            }

                            this.C = v10;
                            LastReadDataManager v2_5 = android.support.design.a.j();
                            v5_1 = this.mCategoryInfo.mCategoryName;
                            if (!android.support.design.a.isTextEmpty(v5_1)) {
                                Logger.d("LastReadDataManager", "clearLastReadCell:" + v5_1);
                                if (v2_5.a.containsKey(v5_1)) {
                                    v2_5.a.put(v5_1, null);
                                }

                                v5_2 = v2_5.b.edit();
                                v5_2.putString("sp_key_last_read", LastReadDataManager.a(v2_5.a));
                                SharedPreferenceCompat.commit(v5_2);
                            }

                            v2_5 = android.support.design.a.j();
                            v5_1 = this.mCategoryInfo.mCategoryName;
                            v14 = this.B;
                            boolean v6_2 = articleQueryObj.hasMoreToRefresh;
                            v16 = articleQueryObj.minBehotTime;
                            v18 = this.C;
                            long v20 = articleQueryObj.o;
                            if (android.support.design.a.isTextEmpty(v5_1)) {

                            } else if (v14 < 0) {

                            } else {
                                Logger.d("LastReadDataManager", "setLastestReadCell;category:" + v5_1 + ";readTime:" + v14);
                                LastReadDataManager.LastReadData v8 = new LastReadDataManager.LastReadData();
                                v8.time = v14;
                                v8.categoryId = v5_1;
                                v8.clickable = v6_2;
                                v8.behotTime = v16;
                                v8.nextBehotTime = v18;
                                v8.cursor = v20;
                                v2_5.a.put(v5_1, v8);
                                v5_2 = v2_5.b.edit();
                                v5_2.putString("sp_key_last_read", LastReadDataManager.a(v2_5.a));
                                SharedPreferenceCompat.commit(v5_2);
                            }
                        }
                    }
                }
                if (v10 <= 0) {

                } else {
                    AppData.getInstance().a(this.mCategoryInfo.mCategoryCity, v10);
                }
            } else {
                if (!((Boolean) this.isRefresh.get()).booleanValue()) {
                } else if (articleQueryObj.v) {
                } else {
                    AppData.getInstance().a(this.mCategoryInfo.mCategoryCity, System.currentTimeMillis());
                }
            }

            if (((Boolean) this.isRefresh.get()).booleanValue()) {
                this.isRefresh.set(Boolean.valueOf(false));
                v5 = 0;
                if ((v13) && !v7.isEmpty() && articleQueryObj.u > 0) {
                    v14 = System.currentTimeMillis();
                    v16 = AppData.getInstance().L();
                    v18 = articleQueryObj.u;
                    Logger.d("FeedDataProvider", "lastTime is " + v18 + " and interval is " + 1000 * v16 + " and ? " + (v14 - v18));
                    if (v14 - v18 < 1000 * v16) {
                        Logger.d("FeedDataProvider", "lastTime is " + v18 + " and interval is " + v16 * 1000);
                        v2_2 = false;
                    } else {
                        v2_2 = v3_2;
                    }

                    if (v2_2) {
                        AppData.getInstance();
                        AppData.ai();
                        if (v14 < 10000) {
                            Logger.d("FeedDataProvider", "lastApnTime is " + v18 + " and interval is 10s");
                            v2_2 = false;
                            AppData.getInstance();
                            AppData.ah();
                        }
                    }

                    if (this.mCategoryInfo.mReferType == 2) {
                        v3_2 = true;
                    } else {
                        v3_2 = v2_2;
                    }
                }

                if ((this.h()) || (v7.isEmpty())) {
                    v6_3 = v9;
                } else {
                    if (articleQueryObj.minBehotTime > 0 && !articleQueryObj.d && articleQueryObj.hasMore || articleQueryObj.minBehotTime <= 0) {
                        List v5_3 = android.support.design.a.a(this.mCellRefList, v7, false);
                        v2 = v5_3.size();
                        v6 = v5_3.iterator();
                        for (v5 = v2; v6.hasNext(); v5 = v2) {
                            v2_1 = v6.next();
                            v2 = ((CellRef) v2_1).article == null || ((CellRef) v2_1).article.clickTimestamp <= 0 ? v5 : v5 - 1;
                        }

                        if (v5 < 0) {
                            v5 = 0;
                        }

                        this.mCellRefList.clear();
                        v6_3 = 1;
                    } else {
                        v6_3 = v9;
                    }
                }

                if (this.h()) {
                    v2_2 = v4 != 0 ? Spipe.inst().isLogin : articleQueryObj.loginStatus;
                    if (v2_2) {
                        ((ListData) this.h.get()).f = 0;
                        v24 = v7;
                        v7_1 = v6_3;
                        v6_4 = v24;
                    } else {
                        ((ListData) this.h.get()).f = 1;
                        v24 = v7;
                        v7_1 = v6_3;
                        v6_4 = v24;
                    }
                } else {
                    v6_4 = android.support.design.a.a(this.mCellRefList, v7, false);
                    v7_1 = 1;
                    v2 = v6_4.size();
                    Iterator v9_1 = v6_4.iterator();
                    for (v5 = v2; v9_1.hasNext(); v5 = v2) {
                        v2_1 = v9_1.next();
                        v2 = ((CellRef) v2_1).article == null || ((CellRef) v2_1).article.clickTimestamp <= 0 ? v5 : v5 - 1;
                    }

                    if (v5 < 0) {
                        v5 = 0;
                    }

                    ((ListData) this.h.get()).a(articleQueryObj.loginStatus);
                }

                v9 = v6_4.size();
                if (((ListData) this.h.get()).g < articleQueryObj.firstCateBehotTime) {
                    ((ListData) this.h.get()).g = articleQueryObj.firstCateBehotTime;
                }

                if (v9 <= 0) {
                    if ((v13) && articleQueryObj.lastCateBehotTime > 0) {
                        ((ListData) this.h.get()).h = articleQueryObj.lastCateBehotTime;
                    }

                    if (articleQueryObj.d) {
                        if (v13) {
                            ((ListData) this.h.get()).d = false;
                        }
                    } else if (this.mRefArticleRecentFragment.get() != null) {
                        if (articleQueryObj.mAdsAppItem != null) {
                            v2_3 = this.i;
                            AdsAppItem v3_3 = articleQueryObj.mAdsAppItem;
                            v4_1 = new d$b();
                            v4_1.a = v3_3;
                            v2_3.set(v4_1);
                        } else {
                            v2_3 = this.i;
                            d$b v3_4 = new d$b();
                            v3_4.a = null;
                            v3_4.c = true;
                            v2_3.set(v3_4);
                        }
                    }

                    if (v7_1 != 0 || (this.q) || (this.r)) {
                        this.f();
                    }

                    if (this.mRefArticleRecentFragment.get() != null && this.mCategoryInfo.mWendaReferType != -1 && (this.h()) && NetworkUtils.getNetworkType(BaseApplication.getApplication()) != NetworkType.NONE) {
                        ((ArticleRecentFragment) this.mRefArticleRecentFragment.get()).showNoDataView();
                    }

                    this.h.notifyChange();
                    this.d.set(Boolean.valueOf(false));
                    if ((v12) && NetworkUtils.getNetworkType(BaseApplication.getApplication()) != NetworkType.NONE) {
                        this.e();
                    }

                    this.onArticleListReceived(articleQueryObj.isRefreshing, articleQueryObj.v, false, false);
                    return;
                }

                if (articleQueryObj.mAdsAppItem != null) {
                    v2_3 = this.i;
                    AdsAppItem v7_2 = articleQueryObj.mAdsAppItem;
                    d$b v8_1 = new d$b();
                    v8_1.a = v7_2;
                    v2_3.set(v8_1);
                    v2 = 1;
                } else {
                    v2 = 0;
                }

                v7_3 = articleQueryObj.category;
                if (AppData.getInstance().getAbSettings().isFeedClearRefreshEnable()) {
                    ArrayList v7_4 = new ArrayList(this.z);
                    List v8_2 = android.support.design.a.a(((List) v7_4), v6_4, false);
                    this.z.clear();
                    this.z.addAll(((Collection) v8_2));
                    v8_2.addAll(((Collection) v7_4));
                    this.mCellRefList.clear();
                } else if (AppData.getInstance().getAbSettings().isFeedClearAllRefreshEnable()) {
                    this.z.clear();
                    this.z.addAll(((Collection) v6_4));
                    if ("__all__".equals(v7_3)) {
                        v7_1 = AppData.getInstance().getAbSettings().getRefreshHistoryNotifyCount();
                        if (v7_1 < 0) {
                            a(v6_4);
                        } else if (v7_1 != 0) {
                            SharedPreferences v8_3 = BaseApplication.getApplication().getSharedPreferences("has_refresh_history_entry_show", 0);
                            int v11 = v8_3.getInt("has_show_count", 0);
                            if (v11 < v7_1) {
                                SharedPreferences.Editor v7_5 = v8_3.edit();
                                v7_5.putInt("has_show_count", v11 + 1);
                                v7_5.apply();
                                a(v6_4);
                            } else {
                            }
                        } else {
                        }
                        this.mCellRefList.clear();
                    } else {
                        this.mCellRefList.clear();
                    }
                }

                v6_4.addAll(this.mCellRefList);
                this.mCellRefList.clear();
                if (v4 != 0) {
                    v2_1 = this.h.get();
                    v4_2 = !v13 || !articleQueryObj.t ? true : false;
                    ((ListData) v2_1).e = v4_2;
                    ((ListData) this.h.get()).d = true;
                    ((ListData) this.h.get()).c = true;
                } else {
                    if (v5 < 0 || v5 > v9) {
                        v5 = v9;
                    }

                    if (v5 > 0 && v2 == 0 && !this.mCategoryInfo.mIsFromConcernDetailVideo && this.mRefArticleRecentFragment.get() != null) {
                        this.i.set(d$b.a(String.format(getString(com.ss.android.article.browser.R.string.rk), Integer.valueOf(v5))));
                    }

                    if (!articleQueryObj.hasMore) {
                    } else {
                        ((ListData) this.h.get()).e = false;
                        ((ListData) this.h.get()).d = true;
                        ((ListData) this.h.get()).c = true;
                    }
                }
                if (articleQueryObj.lastCateBehotTime > 0) {
                    ((ListData) this.h.get()).h = articleQueryObj.lastCateBehotTime;
                }
                v4_2 = false;
            } else {
                v2_2 = false;
                if ((articleQueryObj.d) || (articleQueryObj.v)) {
                    v2_2 = true;
                }

                v6_4 = android.support.design.a.a(this.mCellRefList, v7, v2_2);
                if (articleQueryObj.d) {
                    v4_2 = !v6_4.isEmpty() ? true : false;
                    ((ListData) this.h.get()).d = v4_2;
                    if (!v4_2) {
                    } else {
                        ((ListData) this.h.get()).e = true;
                    }
                } else {
                    v4_2 = articleQueryObj.hasMore;
                    ((ListData) this.h.get()).c = v4_2;
                    ((ListData) this.h.get()).a(articleQueryObj.loginStatus);
                    this.m = articleQueryObj.respOffset;
                }
                if (articleQueryObj.lastCateBehotTime <= 0) {
                } else if (((ListData) this.h.get()).h <= articleQueryObj.lastCateBehotTime && !AppData.getInstance().getAbSettings().isFeedLoadMoreNewData()) {
                } else {
                    ((ListData) this.h.get()).h = articleQueryObj.lastCateBehotTime;
                }
            }

            if (!v6_4.isEmpty()) {
                this.mCellRefList.addAll(v6_4);
                this.f();
            } else {
                this.a(v4_2);
                this.f();
            }

            if (articleQueryObj.subEntranceList != null && !articleQueryObj.T) {
                this.f.clear();
                this.f.addAll(articleQueryObj.subEntranceList);
            }

            this.h.notifyChange();
            this.d.set(Boolean.valueOf(false));
            if (!v3_2 || NetworkUtils.getNetworkType(BaseApplication.getApplication()) == NetworkType.NONE) {
                this.p = v3_2;
                if (!this.h()) {
                    long v4_3 = ((ListData) this.h.get()).g;
                    AppData v6_5 = AppData.getInstance();
                    v7_3 = this.mCategoryInfo.mCategoryCity;
                    if (!android.support.design.a.isTextEmpty(v7_3)) {
                        v2_1 = v6_5.bv.get(v7_3);
                        if (v2_1 == null) {
                            AppData.b v2_6 = new AppData.b();
                            v2_6.b = v4_3;
                            v6_5.bv.put(v7_3, v2_6);
                        } else if (((AppData.b) v2_1).b != v4_3) {
                            ((AppData.b) v2_1).b = v4_3;
                            ((AppData.b) v2_1).f = null;
                            ((AppData.b) v2_1).g = null;
                            ((AppData.b) v2_1).c = 0;
                            ((AppData.b) v2_1).d = 0;
                        }
                    }
                }
            } else {
                this.p = false;
                this.e();
            }

            this.onArticleListReceived(articleQueryObj.isRefreshing, articleQueryObj.v, v3_2, true);
        }
    }

    private void b(ArticleQueryObj arg14) {
        String v0_2 = null;
        JSONException v1_1;
        String v1;
        long v0;
        long v2 = 0;
        if (arg14 != null) {
            long v6 = System.currentTimeMillis() + 200;
            if (arg14.maxBehotTime > v2) {
                if (this.w > v2) {
                    v0 = v6 - this.w;
                } else {
                    v0 = v2;
                }
            } else if (this.t > v2) {
                v0 = v6 - this.t;
            } else if (this.v > v2) {
                v0 = v6 - this.v;
            } else if (this.E > v2) {
                v0 = v6 - this.E;
            } else {
                v0 = v2;
            }

            String v4 = "channel_fetch";
            if (arg14 != null && (arg14.netRequestTime > v2 || arg14.localLoadTime > v2) && v0 > v2) {
                JSONObject v5 = new JSONObject();
                try {
                    if (this.mCategoryInfo.mReferType == 2) {
                        v5.put("concern_id", this.c.get());
                        v5.put("category", this.c.get());
                    } else {
                        v5.put("category", this.mCategoryInfo.mCategoryName);
                    }

                    if (arg14.maxBehotTime > v2) {
                        v5.put("is_refresh", 0);
                    } else {
                        v5.put("is_refresh", 1);
                    }

                    if (arg14.errorOccur) {
                        v5.put("has_retryed", 1);
                    } else {
                        v5.put("has_retryed", 0);
                    }

                    if (this.E > v2) {
                        v5.put("is_prefetch", 1);
                    } else {
                        v5.put("is_prefetch", 0);
                    }

                    v5.put("total", v0);
                    if (arg14.maxBehotTime <= v2 && this.t > v2 && this.u > this.t) {
                        v5.put("gesture_duration", this.u - this.t);
                    }

                    if (arg14.netRequestTime > v2) {
                        v5.put("gesture_to_net", arg14.startRequestTimeStamp - this.u);
                        v5.put("network", arg14.netRequestTime);
                        v5.put("net_to_parse", arg14.responseStartParseTs - arg14.finishRequestTimeStamp);
                        v5.put("parse_data_duration", arg14.responseParseCost);
                        v5.put("parse_to_db", arg14.updateDbStartTs - arg14.responseFinishParseTs);
                        v5.put("db_duration", arg14.updateDbCost);
                    }

                    if (arg14.localLoadTime > v2) {
                        v1 = "channel_fetch_local";
                        v5.put("local", arg14.localLoadTime);
                        v0_2 = v1;
                    } else {
                        v0_2 = v4;
                    }
                    if (arg14.maxBehotTime <= v2) {
                    } else {
                        v5.put("db_to_show", v6 - arg14.updateDbEndTs);
                    }
                } catch (JSONException ex) {
                    ex.printStackTrace();
                }
                com.bytedance.article.common.a.g.a(v0_2, v5);
                if (!Logger.debug()) {

                } else {
                    Logger.d(v0_2, v5.toString());
                }
            }

            this.t = v2;
            this.v = v2;
            this.u = v2;
            this.E = v2;
            this.w = v2;
        }
    }

    private static void b(List arg6) {
        int v0_3;
        if (arg6 != null) {
            Iterator v3 = arg6.iterator();
            while (v3.hasNext()) {
                Object v0 = v3.next();
                if (v0 == null) {
                    continue;
                }

                if (((CellRef) v0).cellType == 0) {
                    Article v0_1 = ((CellRef) v0).article;
                    if (v0_1 == null) {
                        continue;
                    }

                    AbsApplication v4 = BaseApplication.getApplication();
                    boolean v0_2 = v0_1.aF != null ? v0_1.aF.a(((Context) v4)) : false;
                    if (!v0_2) {
                        v0_3 = 0;
                    } else {
                        v0_3 = 1;
                    }
                } else {
                    if (((CellRef) v0).cellType == 10) {
                        DownloadAppAd v0_4 = ((CellRef) v0).downloadAppAd;
                        if (v0_4 != null && (v0_4.isValid()) && (!ToolUtils.isAppInstalled(BaseApplication.getApplication(), v0_4.pkg) || !v0_4.a(BaseApplication.getApplication()))) {
                            v0_3 = 0;
                        } else {
                            v0_3 = 1;
                        }
                    } else {
                        v0_3 = 0;
                    }
                }
                if (v0_3 == 0) {
                    continue;
                }
                v3.remove();
            }
        }
    }

    public final boolean b() {
        boolean v0 = System.currentTimeMillis() - this.A > 1000 ? true : false;
        return v0;
    }

    public final void c() {
        if (this.s) {
            this.s = false;
            if (this.D != null) {
                Iterator v9 = this.D.iterator();
                while (v9.hasNext()) {
                    Object v0 = v9.next();
                    MobClickCombiner.onEvent(AbsApplication.getCtx(), ((a) v0).a, ((a) v0).b, ((a) v0).c, 0, ((a) v0).d);
                }

                this.D.clear();
            }
        }
    }

    public final void d() {
        this.F.clear();
        this.F.addAll(this.mCellRefList);
        this.mCellRefList.clear();
        int v0;
        for (v0 = 0; v0 < 6; ++v0) {
            this.mCellRefList.add(new CellRef(-10));
        }

        this.f();
        this.h.notifyChange();
        this.q = true;
    }

    private void e() {
        boolean v0 = true;
        this.isRefresh.set(Boolean.valueOf(true));
        FeedRefreshFromInfo info = FeedRefreshFromInfo.create(0, "");
        if (this.mRefArticleRecentFragment.get() != null) {
            v0 = false;
        }
        info.c = v0;
        this.cancelPreviousQueryThread();
        this.a(info, false);
    }

    private int f() {
        return this.mRefArticleRecentFragment.get() != null ? ((c) this.mRefArticleRecentFragment.get()).refreshList() : 0;
    }

    private boolean g() {
        return this.mCategoryInfo.mLastReadLocalEnable && AppData.getInstance().lastReadRefresh();
    }

    private boolean h() {
        return (this.mCellRefList.isEmpty()) || (this.q);
    }
}

